package seqlist.oj;

/**
 * @Author: yuisama
 * @Date: 2019/12/9 11:49
 * @Description:课堂练习第四题，输出链表倒数第K个节点,快慢指针
 */
public class Solution4 {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head == null || k <= 0) {
            return null;
        }
        ListNode fast = head;
        ListNode low = head;
        int count = 0;
        while (fast != null) {
            fast = fast.next;
            count ++;
            if (count >= k + 1) {
                low = low.next;
            }
        }
        if (k > count) {
            return null;
        }else {
            return low;
        }
    }
}
